#include "dlgmodifyinfo.h"
#include "ui_dlgmodifyinfo.h"
#include "sql.h"
#include <QDebug>
#include <QMessageBox>
#include <QRegExpValidator>

DlgModifyInfo::DlgModifyInfo(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgModifyInfo)
{
    ui->setupUi(this);
    //使每次弹窗都在最前面
    setWindowFlag(Qt::WindowStaysOnTopHint);

    //书籍ISBN为数字
    QRegExpValidator *pReg = new QRegExpValidator(QRegExp("[0-9]{30}"), this);
    ui->leBookISBN->setValidator(pReg);
}

DlgModifyInfo::~DlgModifyInfo()
{
    delete ui;
}

void DlgModifyInfo::showInfo(QMap<QString, QString> infoMap)
{
    ui->leBookname->setText(infoMap["书名"]);
    ui->leBooktype->setText(infoMap["类型"]);
    ui->lebookpath->setText(infoMap["路径"]);

    ui->leAuthor->setText(infoMap["作者"]);
    ui->lePublisher->setText(infoMap["出版社"]);
    ui->lePublishyear->setText(infoMap["出版年份"]);
    ui->leBookISBN->setText(infoMap["ISBN号"]);

    ui->leBooklabel->setText(infoMap["标签类型"]);
    ui->teBookintro->setText(infoMap["简介"]);

    m_bookInfo.bookname = infoMap["书名"];
    m_bookInfo.booktype = infoMap["类型"];
    m_bookInfo.bookpath = infoMap["路径"];
}


void DlgModifyInfo::on_ptnModConfirm_clicked()
{
    m_bookInfo.author = ui->leAuthor->text();
    m_bookInfo.publisher = ui->lePublisher->text();
    m_bookInfo.publishyear = ui->lePublishyear->text();
    m_bookInfo.bookisbn = ui->leBookISBN->text();
    m_bookInfo.bookintroduction = ui->teBookintro->toPlainText();

    QString booklabel = ui->leBooklabel->text();;
    //多个标签处理，相当于去重
    QString labelStr = "";
    if (booklabel.length() > 0)
    {
        QStringList addlabelList = booklabel.split(";");
        for (int i = 0; i < addlabelList.size(); ++i)
        {
            QString label = addlabelList[i].trimmed();
            if (label.length() > 0)
            {
                labelStr.append(addlabelList[i]);
                if (i != addlabelList.size()-1)
                {
                    labelStr.append(";");
                }
            }
        }
    }
    m_bookInfo.booklabel = labelStr;

    Sql::getInstance()->updateBookInfo(m_bookInfo);

    emit modifyConfirm(m_bookInfo);

    QMessageBox::information(this,"信息","修改成功");
}

void DlgModifyInfo::on_penModCancel_clicked()
{
    this->hide();
}
